package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.po.App;
import com.dmap.admin.po.Company;
import com.dmap.admin.service.backgroundv2.AppService;
import com.dmap.admin.service.backgroundv2.CompanyService;
import com.dmap.admin.service.backgroundv2.CompanyStructureService;
import com.dmap.admin.vo.backgroundv2.CompanyStructureVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 01/06/18
 * \* Description:公司组织结构接口实现类
 * \
 */
@Service
public class CompanyStructureServiceImpl implements CompanyStructureService {

    @Resource
    private CompanyService companyService;
    @Resource
    private AppService appService;

    @Override
    public CompanyStructureVo selectByCompanyId(Long companyId, Integer companyType) {
        Company rootCompany = companyService.selectById(companyId);
        if (rootCompany == null) {
            return null;
        }
        CompanyStructureVo companyStructureVo = new CompanyStructureVo();
        companyStructureVo.setId(rootCompany.getId().toString());
        companyStructureVo.setLabel(rootCompany.getName());
        //总公司则返回总公司/分公司/项目三级
        if (companyType.equals(AdminConstant.COMPANY_TYPE_HEAD)) {
            List<Company> childrenList = companyService.selectChildrenById(rootCompany.getId());
            List<CompanyStructureVo> companyChildList = new ArrayList<>();
            for (Company child : childrenList) {
                CompanyStructureVo childCompany = new CompanyStructureVo();
                childCompany.setId(child.getId().toString());
                childCompany.setLabel(child.getName());

                List<App> appList = appService.selectByCompanyId(child.getId());
                List<CompanyStructureVo> appChildrenList = new ArrayList<>();
                for (App app : appList) {
                    CompanyStructureVo childApp = new CompanyStructureVo();
                    childApp.setId(app.getId().toString());
                    childApp.setLabel(app.getProjectName());
                    appChildrenList.add(childApp);
                }
                childCompany.setChildren(appChildrenList);
                companyChildList.add(childCompany);
            }
            companyStructureVo.setChildren(companyChildList);
            //分公司则返回分公司/项目二级
        } else if (companyType.equals(AdminConstant.COMPANY_TYPE_BRANCH)) {
            List<App> appList = appService.selectByCompanyId(rootCompany.getId());
            List<CompanyStructureVo> appChildrenList = new ArrayList<>();
            for (App app : appList) {
                CompanyStructureVo childApp = new CompanyStructureVo();
                childApp.setId(app.getId().toString());
                childApp.setLabel(app.getProjectName());
                appChildrenList.add(childApp);
            }
            companyStructureVo.setChildren(appChildrenList);
        }
        return companyStructureVo;
    }

}